package com.heima.feigns.fallback;

import com.heima.feigns.UserFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 远程调用user微服务时出现错误时的应急处理类
 * @Author XHui
 * @Since 2024/3/4 15:33
 * @Version 1.0
 */
@Slf4j
@Component
public class UserFeignFallback implements FallbackFactory<UserFeign> {

    /**
     * 创造
     *
     * @param throwable 可投掷
     * @return 用户佯装
     */
    @Override
    public UserFeign create(Throwable throwable) {
        throwable.printStackTrace();
        return new UserFeign() {
            @Override
            public ResponseResult<ApUser> findUserById(Integer id) {
                log.error("Feign服务降级触发 远程调用:UserFeign  findUserById 失败,参数:{}",id);
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"远程服务调用出现异常");
            }
        };
    }
}
